#include "pausemenu.h"
#include "ui_pausemenu.h"

PauseMenu::PauseMenu(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PauseMenu)
{
    ui->setupUi(this);
}

PauseMenu::~PauseMenu()
{
    delete ui;
}

void PauseMenu::on_goBtn_clicked()
{
    dynamic_cast<Game*>(this->parent())->show();
    dynamic_cast<Game*>(this->parent())->StartTimers();
    this->deleteLater();
}

void PauseMenu::on_saveBtn_clicked()
{
    Game *g = dynamic_cast<Game*>(this->parent());

    QString filename = QFileDialog::getSaveFileName(this, "Save Form", "Saves", "");
    QFile file(filename);

    if (!file.open(QIODevice::WriteOnly)){
        QMessageBox::information(this, tr("Unable to open file"), file.errorString());
        return;
    }

    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_4_6); // Change this based on current Qt version

    out << g->levelN;  //level
    out << g->x;  //location
    out << g->y;
    out << g->ymomentum; //speed
    out << g->timeCnt; //Time spent
    out << g->deathCnt; //Deaths

    file.close();
}

void PauseMenu::on_loadBtn_clicked()
{
    Game *g = dynamic_cast<Game*>(this->parent());

    QString filename = QFileDialog::getOpenFileName(this, "Load Form", "Saves", "");
    QFile file(filename);

    if (!file.open(QIODevice::ReadOnly)){
        QMessageBox::information(this, tr("Unable to load file"), file.errorString());
        return;
    }

    QDataStream in(&file);
    in.setVersion(QDataStream::Qt_4_6); // Change this based on current Qt version

    int l,x,y,ym,t,d;

    in >> l;  //level
    in >> x;  //location
    in >> y;
    in >> ym; //speed
    in >> t; //Time spent
    in >> d; //Deaths

    file.close();

    g->LoadGame(l,x,y,ym,t,d);
    g->StartTimers();
    this->deleteLater();
}

void PauseMenu::on_stopBtn_clicked()
{
    MainMenu *m = dynamic_cast<MainMenu*>(this->parent()->parent());
    dynamic_cast<Game*>(this->parent())->deleteLater();
    this->deleteLater();

    m->show();
}
